package com.colorful.insuranceproject.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;


/**
 * 数字键盘输入法
 */
public class NumericPasswordInputMethod extends FrameLayout {

    private TextView mKeyZero;
    private TextView mKeyOne;
    private TextView mKeyTwo;
    private TextView mKeyThree;
    private TextView mKeyFour;
    private TextView mKeyFive;
    private TextView mKeySix;
    private TextView mKeySeven;
    private TextView mKeyEight;
    private TextView mKeyNine;
    private OnClickListener keywordClickListener;
    private ImageView ivDel;

    private OnKeyListener keyListener;

    public NumericPasswordInputMethod(Context context) {
        this(context, null);
    }

    public NumericPasswordInputMethod(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumericPasswordInputMethod(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        inflater.inflate(R.layout.view_numeric_password_input_method, this, true);

        mKeyZero = (TextView) findViewById(R.id.tv_view_numeric_password_input_method_zero);
        mKeyOne = (TextView) findViewById(R.id.tv_view_numeric_password_input_method_one);
        mKeyTwo = (TextView) findViewById(R.id.tv_view_numeric_password_input_method_two);
        mKeyThree = (TextView) findViewById(R.id.tv_view_numeric_password_input_method_three);
        mKeyFour = (TextView) findViewById(R.id.tv_view_numeric_password_input_method_four);
        mKeyFive = (TextView) findViewById(R.id.tv_view_numeric_password_input_method_five);
        mKeySix = (TextView) findViewById(R.id.tv_view_numeric_password_input_method_six);
        mKeySeven = (TextView) findViewById(R.id.tv_view_numeric_password_input_method_seven);
        mKeyEight = (TextView) findViewById(R.id.tv_view_numeric_password_input_method_eight);
        mKeyNine = (TextView) findViewById(R.id.tv_view_numeric_password_input_method_nine);

        ivDel = (ImageView) findViewById(R.id.iv_view_numeric_password_input_method_del);

        keywordClickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_view_numeric_password_input_method_zero:
                        onKeyEvent(KeyEvent.KEYCODE_0);
                        break;
                    case R.id.tv_view_numeric_password_input_method_one:
                        onKeyEvent(KeyEvent.KEYCODE_1);
                        break;
                    case R.id.tv_view_numeric_password_input_method_two:
                        onKeyEvent(KeyEvent.KEYCODE_2);
                        break;
                    case R.id.tv_view_numeric_password_input_method_three:
                        onKeyEvent(KeyEvent.KEYCODE_3);
                        break;
                    case R.id.tv_view_numeric_password_input_method_four:
                        onKeyEvent(KeyEvent.KEYCODE_4);
                        break;
                    case R.id.tv_view_numeric_password_input_method_five:
                        onKeyEvent(KeyEvent.KEYCODE_5);
                        break;
                    case R.id.tv_view_numeric_password_input_method_six:
                        onKeyEvent(KeyEvent.KEYCODE_6);
                        break;
                    case R.id.tv_view_numeric_password_input_method_seven:
                        onKeyEvent(KeyEvent.KEYCODE_7);
                        break;
                    case R.id.tv_view_numeric_password_input_method_eight:
                        onKeyEvent(KeyEvent.KEYCODE_8);
                        break;
                    case R.id.tv_view_numeric_password_input_method_nine:
                        onKeyEvent(KeyEvent.KEYCODE_9);
                        break;
                    case R.id.iv_view_numeric_password_input_method_del:
                        onKeyEvent(KeyEvent.KEYCODE_DEL);
                        break;

                }
            }
        };

        mKeyZero.setOnClickListener(keywordClickListener);
        mKeyOne.setOnClickListener(keywordClickListener);
        mKeyTwo.setOnClickListener(keywordClickListener);
        mKeyThree.setOnClickListener(keywordClickListener);
        mKeyFour.setOnClickListener(keywordClickListener);
        mKeyFive.setOnClickListener(keywordClickListener);
        mKeySix.setOnClickListener(keywordClickListener);
        mKeySeven.setOnClickListener(keywordClickListener);
        mKeyEight.setOnClickListener(keywordClickListener);
        mKeyNine.setOnClickListener(keywordClickListener);

        ivDel.setOnClickListener(keywordClickListener);

    }

    @Override
    public void setOnKeyListener(OnKeyListener l) {
        this.keyListener = l;
    }

    void onKeyEvent(int keyCode) {
        KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
        KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
        if (keyListener != null) {
            keyListener.onKey(this, keyCode, downEvent);
            keyListener.onKey(this, keyCode, upEvent);
        }
    }
}
